﻿using G_IS.Comm;
using G_IS.Extensions;
using Plusii.iiWeb;
using R.DMS.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace R.DMS.UI.Web
{
    public partial class ContractDetail : iiDialog
    {
        CommonBusiness service = new CommonBusiness();

        #region 属性

        /// <summary>
        /// 当前ID
        /// </summary>
        private string CurrentID
        {
            get
            {
                object tempObject = ViewState["CurrentID"];
                return (tempObject != null) ? (string)tempObject : string.Empty;
            }
            set
            {
                ViewState["CurrentID"] = value;
            }
        }

        #endregion

        /// <summary>
        /// 页面加载
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                #region 验证权限

                #endregion

                #region 获取参数

                string id = Request.QueryString["ID"];
                if (id != null && id.Trim().Length != 0)
                {
                    this.CurrentID = id.Trim();
                }

                #endregion

                //初始化
                this.Initialize();
            }

            #region 页面标题

            this.Title = PageTitle.Text;

            #endregion

            #region 页面要素

            this.AssignDialogCallback(CancelButton, "'Close'", "window");

            #endregion
        }

        #region 初始化

        /// <summary>
        /// 初始化
        /// </summary>
        private void Initialize()
        {
            #region 页面内容

            txtContractName.Text = string.Empty;
            txtCustomerName.Text = string.Empty;
            txtCustomerNo.Text = string.Empty;

            var list = EnumHelper.EnumToEnumInfoList(typeof(EnumContractStatus));
            drpStatus.Items.Clear();
            //drpStatus.Items.Add(string.Empty);
            list.ForEach(t =>
            {
                drpStatus.Items.Add(new ListItem(t.itemDescription, t.itemValue));
            });

            #endregion

            if (this.CurrentID.Length == 0)
            {
                //新增
            }
            else
            {
                //编辑
                this.BindItem();
            }
        }

        #endregion

        #region 绑定数据

        /// <summary>
        /// 绑定项目
        /// </summary>
        private void BindItem()
        {
            var item = service.GetContract(CurrentID.ToInt());
            if (item != null)
            {
                txtContractName.Text = item.C_ContractName;
                txtCustomerName.Text = item.C_CustomerName;
                txtCustomerNo.Text = item.C_CustomerNo;
                drpStatus.SelectedValue = item.C_Status.ToString();
            }
        }

        #endregion

        #region 操作

        /// <summary>
        /// 确定
        /// </summary>
        protected void OKButton_Click(object sender, EventArgs e)
        {
            if (this.CurrentID.Length == 0)
            {
                //新增
                try
                {
                    var entity = new ContractEntity
                    {
                        C_ContractName = txtContractName.Text.Trim(),
                        C_CustomerName = txtCustomerName.Text.Trim(),
                        C_CustomerNo = Request.Form["txtCustomerNo"].Trim(),
                        C_Status = (byte)drpStatus.SelectedValue.ToInt(),
                        C_DeleteFlag = false,
                        C_CreateTime = DateTime.Now,
                        C_CreatorUserId = LocalGlobal.CurrentUserID,
                        C_CreatorUserName = LocalGlobal.CurrentUser
                    };

                    service.CreateContract(entity);
                }
                catch (Exception error)
                {
                    this.ShowErrorMessage(this.GetGlobalResourceString("CreateErrorMessage") + error.Message);
                    return;
                }
            }
            else
            {
                //编辑
                try
                {
                    var entity = service.GetContract(CurrentID.ToInt());
                    if (entity != null)
                    {
                        entity.C_ContractName = txtContractName.Text.Trim();
                        entity.C_CustomerName = txtCustomerName.Text.Trim();
                        entity.C_CustomerNo = Request.Form["txtCustomerNo"].Trim();
                        entity.C_Status = (byte)drpStatus.SelectedValue.ToInt();
                        entity.C_UpdateTime = DateTime.Now;
                        entity.C_UpdateUserId = LocalGlobal.CurrentUserID;
                        entity.C_UpdateUserName = LocalGlobal.CurrentUser;

                        service.UpdateContract(entity);
                    }
                }
                catch (Exception error)
                {
                    this.ShowErrorMessage(this.GetGlobalResourceString("UpdateErrorMessage") + error.Message);
                    return;
                }
            }

            //回调
            this.DialogCallback("'CloseRefresh'", "window");
        }

        #endregion
    }
}